<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>处理请求 | 课程简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C03-RequestAndResponse/HandleResponse.html" />
    
    
    <link rel="prev" href="../C03-RequestAndResponse/index.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="4.1"
        data-chapter-title="处理请求"
        data-filepath="C03-RequestAndResponse/HandleRequest.md"
        data-basepath=".."
        data-revision="Sat May 11 2019 20:06:35 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutFlask.html">
            
                
                    <a href="../AboutFlask.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Flask介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/HelloWorld.html">
            
                
                    <a href="../C01-CreateProject/HelloWorld.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        HelloWorld程序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/AboutParams.html">
            
                
                    <a href="../C01-CreateProject/AboutParams.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        参数说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/StartDevelopmentServer.html">
            
                
                    <a href="../C01-CreateProject/StartDevelopmentServer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        开发服务器运行方式
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-RouterAndBlueprint/index.html">
            
                
                    <a href="../C02-RouterAndBlueprint/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        路由与蓝图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-RouterAndBlueprint/Router.html">
            
                
                    <a href="../C02-RouterAndBlueprint/Router.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        路由
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-RouterAndBlueprint/Blueprint.html">
            
                
                    <a href="../C02-RouterAndBlueprint/Blueprint.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        蓝图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="4.1" data-path="C03-RequestAndResponse/HandleRequest.html">
            
                
                    <a href="../C03-RequestAndResponse/HandleRequest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        处理请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/HandleResponse.html">
            
                
                    <a href="../C03-RequestAndResponse/HandleResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        处理响应
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/CookieAndSession.html">
            
                
                    <a href="../C03-RequestAndResponse/CookieAndSession.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie与Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-HookAndContext/index.html">
            
                
                    <a href="../C04-HookAndContext/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        请求钩子与上下文
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-HookAndContext/AbortAndErrorhandler.html">
            
                
                    <a href="../C04-HookAndContext/AbortAndErrorhandler.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-HookAndContext/Hook.html">
            
                
                    <a href="../C04-HookAndContext/Hook.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        请求钩子
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="C04-HookAndContext/Context.html">
            
                
                    <a href="../C04-HookAndContext/Context.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上下文
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Flask-RESTful/index.html">
            
                
                    <a href="../C05-Flask-RESTful/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        Flask-RESTful
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Flask-RESTful/QuickStart.html">
            
                
                    <a href="../C05-Flask-RESTful/QuickStart.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        起步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C05-Flask-RESTful/AboutView.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        关于视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C05-Flask-RESTful/AboutRequest.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutRequest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        关于请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C05-Flask-RESTful/AboutResponse.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        关于响应
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5904;&#x7406;&#x8BF7;&#x6C42;">&#x5904;&#x7406;&#x8BF7;&#x6C42;</h1>
<h2 id="&#x9700;&#x6C42;">&#x9700;&#x6C42;</h2>
<p>&#x5728;&#x89C6;&#x56FE;&#x7F16;&#x5199;&#x4E2D;&#x9700;&#x8981;&#x8BFB;&#x53D6;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x643A;&#x5E26;&#x7684;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x5982;&#x4F55;&#x624D;&#x80FD;&#x6B63;&#x786E;&#x7684;&#x53D6;&#x51FA;&#x6570;&#x636E;&#x5462;&#xFF1F;</p>
<p>&#x8BF7;&#x6C42;&#x643A;&#x5E26;&#x7684;&#x6570;&#x636E;&#x53EF;&#x80FD;&#x51FA;&#x73B0;&#x5728;HTTP&#x62A5;&#x6587;&#x4E2D;&#x7684;&#x4E0D;&#x540C;&#x4F4D;&#x7F6E;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x53C2;&#x6570;&#x3002;</p>
<h2 id="1-url&#x8DEF;&#x5F84;&#x53C2;&#x6570;&#xFF08;&#x52A8;&#x6001;&#x8DEF;&#x7531;&#xFF09;">1. URL&#x8DEF;&#x5F84;&#x53C2;&#x6570;&#xFF08;&#x52A8;&#x6001;&#x8DEF;&#x7531;&#xFF09;</h2>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x6709;&#x4E00;&#x4E2A;&#x8BF7;&#x6C42;&#x8BBF;&#x95EE;&#x7684;&#x63A5;&#x53E3;&#x5730;&#x5740;&#x4E3A;<code>/users/123</code>&#xFF0C;&#x5176;&#x4E2D;123&#x5B9E;&#x9645;&#x4E0A;&#x4E3A;&#x5177;&#x4F53;&#x7684;&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#xFF0C;&#x8868;&#x660E;&#x8BF7;&#x6C42;123&#x53F7;&#x7528;&#x6237;&#x7684;&#x4FE1;&#x606F;&#x3002;&#x6B64;&#x65F6;&#x5982;&#x4F55;&#x4ECE;url&#x4E2D;&#x63D0;&#x53D6;&#x51FA;123&#x7684;&#x6570;&#x636E;&#xFF1F;</p>
<p>Flask&#x4E0D;&#x540C;&#x4E8E;Django&#x76F4;&#x63A5;&#x5728;&#x5B9A;&#x4E49;&#x8DEF;&#x7531;&#x65F6;&#x7F16;&#x5199;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x800C;&#x662F;&#x91C7;&#x7528;&#x8F6C;&#x6362;&#x5668;&#x8BED;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@app.route(&apos;/users/&lt;user_id&gt;&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">user_info</span><span class="hljs-params">(user_id)</span>:</span>
    print(type(user_id))
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello user {}&apos;</span>.format(user_id)
</code></pre>
<p>&#x6B64;&#x5904;&#x7684;<code>&lt;&gt;</code>&#x5373;&#x662F;&#x4E00;&#x4E2A;&#x8F6C;&#x6362;&#x5668;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x7C7B;&#x578B;&#xFF0C;&#x5373;&#x5C06;&#x8BE5;&#x4F4D;&#x7F6E;&#x7684;&#x6570;&#x636E;&#x4EE5;&#x5B57;&#x7B26;&#x4E32;&#x683C;&#x5F0F;&#x8FDB;&#x884C;&#x5339;&#x914D;&#x3001;&#x5E76;&#x4EE5;&#x5B57;&#x7B26;&#x4E32;&#x4E3A;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x7C7B;&#x578B;&#x3001; <code>user_id</code>&#x4E3A;&#x53C2;&#x6570;&#x540D;&#x4F20;&#x5165;&#x89C6;&#x56FE;&#x3002;</p>
<h3 id="flask&#x4E5F;&#x63D0;&#x4F9B;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x7684;&#x8F6C;&#x6362;&#x5668;">Flask&#x4E5F;&#x63D0;&#x4F9B;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x7684;&#x8F6C;&#x6362;&#x5668;</h3>
<pre><code class="lang-python">DEFAULT_CONVERTERS = {
    <span class="hljs-string">&apos;default&apos;</span>:          UnicodeConverter,
    <span class="hljs-string">&apos;string&apos;</span>:           UnicodeConverter,
    <span class="hljs-string">&apos;any&apos;</span>:              AnyConverter,
    <span class="hljs-string">&apos;path&apos;</span>:             PathConverter,
    <span class="hljs-string">&apos;int&apos;</span>:              IntegerConverter,
    <span class="hljs-string">&apos;float&apos;</span>:            FloatConverter,
    <span class="hljs-string">&apos;uuid&apos;</span>:             UUIDConverter,
}
</code></pre>
<p>&#x5C06;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4EE5;&#x6574;&#x578B;&#x5339;&#x914D;&#x6570;&#x636E;&#xFF0C;&#x53EF;&#x4EE5;&#x5982;&#x4E0B;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@app.route(&apos;/users/&lt;int:user_id&gt;&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">user_info</span><span class="hljs-params">(user_id)</span>:</span>
    print(type(user_id))
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello user {}&apos;</span>.format(user_id)


<span class="hljs-decorator">@app.route(&apos;/users/&lt;int(min=1):user_id&gt;&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">user_info</span><span class="hljs-params">(user_id)</span>:</span>
    print(type(user_id))
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello user {}&apos;</span>.format(user_id)
</code></pre>
<h3 id="&#x81EA;&#x5B9A;&#x4E49;&#x8F6C;&#x6362;&#x5668;">&#x81EA;&#x5B9A;&#x4E49;&#x8F6C;&#x6362;&#x5668;</h3>
<p>&#x5982;&#x679C;&#x9047;&#x5230;&#x9700;&#x8981;&#x5339;&#x914D;&#x63D0;&#x53D6;<code>/sms_codes/18512345678</code>&#x4E2D;&#x7684;&#x624B;&#x673A;&#x53F7;&#x6570;&#x636E;&#xFF0C;Flask&#x5185;&#x7F6E;&#x7684;&#x8F6C;&#x6362;&#x5668;&#x5C31;&#x65E0;&#x6CD5;&#x6EE1;&#x8DB3;&#x9700;&#x6C42;&#xFF0C;&#x6B64;&#x65F6;&#x9700;&#x8981;&#x81EA;&#x5B9A;&#x4E49;&#x8F6C;&#x6362;&#x5668;&#x3002;</p>
<h4 id="&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;">&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;</h4>
<p>&#x81EA;&#x5B9A;&#x4E49;&#x8F6C;&#x6362;&#x5668;&#x4E3B;&#x8981;&#x505A;3&#x6B65;</p>
<ol>
<li><p>&#x521B;&#x5EFA;&#x8F6C;&#x6362;&#x5668;&#x7C7B;&#xFF0C;&#x4FDD;&#x5B58;&#x5339;&#x914D;&#x65F6;&#x7684;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> werkzeug.routing <span class="hljs-keyword">import</span> BaseConverter

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MobileConverter</span><span class="hljs-params">(BaseConverter)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x624B;&#x673A;&#x53F7;&#x683C;&#x5F0F;
    &quot;&quot;&quot;</span>
    regex = <span class="hljs-string">r&apos;1[3-9]\d{9}&apos;</span>
</code></pre>
<ul>
<li>&#x6CE8;&#x610F;<code>regex</code>&#x540D;&#x5B57;&#x56FA;&#x5B9A;</li>
</ul>
</li>
<li><p>&#x5C06;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x8F6C;&#x6362;&#x5668;&#x544A;&#x77E5;Flask&#x5E94;&#x7528;</p>
<pre><code class="lang-python">app = Flask(__name__)

<span class="hljs-comment"># &#x5C06;&#x81EA;&#x5B9A;&#x4E49;&#x8F6C;&#x6362;&#x5668;&#x6DFB;&#x52A0;&#x5230;&#x8F6C;&#x6362;&#x5668;&#x5B57;&#x5178;&#x4E2D;&#xFF0C;&#x5E76;&#x6307;&#x5B9A;&#x8F6C;&#x6362;&#x5668;&#x4F7F;&#x7528;&#x65F6;&#x540D;&#x5B57;&#x4E3A;: mobile</span>
app.url_map.converters[<span class="hljs-string">&apos;mobile&apos;</span>] = MobileConverter
</code></pre>
</li>
<li><p>&#x5728;&#x4F7F;&#x7528;&#x8F6C;&#x6362;&#x5668;&#x7684;&#x5730;&#x65B9;&#x5B9A;&#x4E49;&#x4F7F;&#x7528;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@app.route(&apos;/sms_codes/&lt;mobile:mob_num&gt;&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_sms_code</span><span class="hljs-params">(mob_num)</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;send sms code to {}&apos;</span>.format(mob_num)
</code></pre>
</li>
</ol>
<h2 id="2-&#x5176;&#x4ED6;&#x53C2;&#x6570;">2. &#x5176;&#x4ED6;&#x53C2;&#x6570;</h2>
<p>&#x5982;&#x679C;&#x60F3;&#x8981;&#x83B7;&#x53D6;&#x5176;&#x4ED6;&#x5730;&#x65B9;&#x4F20;&#x9012;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;Flask&#x63D0;&#x4F9B;&#x7684;<strong>request</strong>&#x5BF9;&#x8C61;&#x6765;&#x8BFB;&#x53D6;&#x3002;</p>
<p>&#x4E0D;&#x540C;&#x4F4D;&#x7F6E;&#x7684;&#x53C2;&#x6570;&#x90FD;&#x5B58;&#x653E;&#x5728;request&#x7684;&#x4E0D;&#x540C;&#x5C5E;&#x6027;&#x4E2D;</p>
<table>
<thead>
<tr>
<th style="text-align:left">&#x5C5E;&#x6027;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
<th style="text-align:left">&#x7C7B;&#x578B;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">data</td>
<td style="text-align:left">&#x8BB0;&#x5F55;&#x8BF7;&#x6C42;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x8F6C;&#x6362;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;</td>
<td style="text-align:left">*</td>
</tr>
<tr>
<td style="text-align:left">form</td>
<td style="text-align:left">&#x8BB0;&#x5F55;&#x8BF7;&#x6C42;&#x4E2D;&#x7684;&#x8868;&#x5355;&#x6570;&#x636E;</td>
<td style="text-align:left">MultiDict</td>
</tr>
<tr>
<td style="text-align:left">args</td>
<td style="text-align:left">&#x8BB0;&#x5F55;&#x8BF7;&#x6C42;&#x4E2D;&#x7684;&#x67E5;&#x8BE2;&#x53C2;&#x6570;</td>
<td style="text-align:left">MultiDict</td>
</tr>
<tr>
<td style="text-align:left">cookies</td>
<td style="text-align:left">&#x8BB0;&#x5F55;&#x8BF7;&#x6C42;&#x4E2D;&#x7684;cookie&#x4FE1;&#x606F;</td>
<td style="text-align:left">Dict</td>
</tr>
<tr>
<td style="text-align:left">headers</td>
<td style="text-align:left">&#x8BB0;&#x5F55;&#x8BF7;&#x6C42;&#x4E2D;&#x7684;&#x62A5;&#x6587;&#x5934;</td>
<td style="text-align:left">EnvironHeaders</td>
</tr>
<tr>
<td style="text-align:left">method</td>
<td style="text-align:left">&#x8BB0;&#x5F55;&#x8BF7;&#x6C42;&#x4F7F;&#x7528;&#x7684;HTTP&#x65B9;&#x6CD5;</td>
<td style="text-align:left">GET/POST</td>
</tr>
<tr>
<td style="text-align:left">url</td>
<td style="text-align:left">&#x8BB0;&#x5F55;&#x8BF7;&#x6C42;&#x7684;URL&#x5730;&#x5740;</td>
<td style="text-align:left">string</td>
</tr>
<tr>
<td style="text-align:left">files</td>
<td style="text-align:left">&#x8BB0;&#x5F55;&#x8BF7;&#x6C42;&#x4E0A;&#x4F20;&#x7684;&#x6587;&#x4EF6;</td>
<td style="text-align:left">*</td>
</tr>
</tbody>
</table>
<p>&#x4F8B;&#x5982; &#x60F3;&#x8981;&#x83B7;&#x53D6;&#x8BF7;&#x6C42;<code>/articles?channel_id=1</code>&#x4E2D;<code>channel_id</code>&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x6309;&#x5982;&#x4E0B;&#x65B9;&#x5F0F;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> request

<span class="hljs-decorator">@app.route(&apos;/articles&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_articles</span><span class="hljs-params">()</span>:</span>
    channel_id = request.args.get(<span class="hljs-string">&apos;channel_id&apos;</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;you wanna get articles of channel {}&apos;</span>.format(channel_id)
</code></pre>
<h4 id="&#x4E0A;&#x4F20;&#x56FE;&#x7247;">&#x4E0A;&#x4F20;&#x56FE;&#x7247;</h4>
<p>&#x5BA2;&#x6237;&#x7AEF;&#x4E0A;&#x4F20;&#x56FE;&#x7247;&#x5230;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x5E76;&#x4FDD;&#x5B58;&#x5230;&#x670D;&#x52A1;&#x5668;&#x4E2D;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> request

<span class="hljs-decorator">@app.route(&apos;/upload&apos;, methods=[&apos;POST&apos;])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">upload_file</span><span class="hljs-params">()</span>:</span>
    f = request.files[<span class="hljs-string">&apos;pic&apos;</span>]
    <span class="hljs-comment"># with open(&apos;./demo.png&apos;, &apos;wb&apos;) as new_file:</span>
    <span class="hljs-comment">#     new_file.write(f.read())</span>
    f.save(<span class="hljs-string">&apos;./demo.png&apos;</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;ok&apos;</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C03-RequestAndResponse/index.html" class="navigation navigation-prev " aria-label="Previous page: 请求与响应"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C03-RequestAndResponse/HandleResponse.html" class="navigation navigation-next " aria-label="Next page: 处理响应"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
